<!--停用启用弹窗-->
<template>
  <div>
    <rk-dialog width="480"
      height="240"
      :title="title"
      :visible="localVisible"
      :destroyOnClose="true"
      @close="handleClose"
      :maskClosable="false">
      <div>
        <el-form-item label="失效时间"
          prop="endtime">
          <el-date-picker v-model="form.endtime"
            clearable
            placeholder="请选择时间"
            format="YYYYMMDD"
            value-format="YYYYMMDD" />
        </el-form-item>
      </div>
      <template #footer>
        <el-button type="primary"
          @click="handleConfirm">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </template>
    </rk-dialog>
  </div>
</template>

<script setup>
import XEUtils from 'xe-utils'
import { AD_STATUS, AD_STATUS_ENUMS } from '@/enums/enums-ad.js'
import { ElMessage, ElLoading } from "element-plus"
import { useZoningApi } from '@/api/index';
const  { updateEleAdStatus } = useZoningApi()

const emits = defineEmits([
  'refresh'
])

const defaultForm = {
  endtime: new Date().format('yyyy-MM-dd')
}

const localVisible = ref(false)
const title = ref('区划')
const form = ref('')
const currentNode = ref('')
form.value = XEUtils.clone(defaultForm, true)
/**
 * 打开弹窗
 */
const handleOpen = (row) => {
  localVisible.value = true
  const { status } = row
  form.value.endtime = (status == AD_STATUS.OPEN) ? new Date().format('yyyy-MM-dd') : '2099-12-31'
  const text = getOpenStopText(row)
  title.value = `${text}区划`
  currentNode.value = row
}

const handleClose = () => {
  form.value = XEUtils.clone(defaultForm, true)
  localVisible.value = false
}

/** 获取启用停用文字描述 */
const getOpenStopText = (status) => {
  let result = (status === AD_STATUS.OPEN)
    ? AD_STATUS_ENUMS[AD_STATUS.STOP].text
    : AD_STATUS_ENUMS[AD_STATUS.OPEN].text
  return result
}

/**提交 */
let loading = null
const handleConfirm = async () => {
  const { guid, status } = currentNode.value
  loading = ElLoading.service({ fullscreen: true, text: getOpenStopText(currentNode.value) + '中,请稍等..' })
  let params = {
    guids: guid,
    status: (status === AD_STATUS.OPEN) ? AD_STATUS.STOP : AD_STATUS.OPEN,
    endtime: form.value.endtime
  }
  const result = await updateEleAdStatus(params)
  const { data, code, msg } = result
  if (code === 200) {
    loading.close()
    handleClose();
    ElMessage({ message: `操作成功！`, type: "success" });
    emits('refresh')
  } else {
    loading.close()
    ElMessage({ message: `操作失败！`, type: "error" });
  }
}

// 对外暴露方法
defineExpose({
  handleOpen
})


</script>

<style lang="scss" scoped>
</style>
<style>
</style>
